<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>需求管理</el-breadcrumb-item>
      <el-breadcrumb-item>需求列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>

      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入需求名搜索"
                    v-model="searchDemands"
                    clearable
                    @clear="getDemandsList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getDemandsList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table :data="demandsList" border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="需求名称" prop="demands"></el-table-column>
        <el-table-column label="发布时间" prop="createdTime"></el-table-column>
        <el-table-column label="截止日期" prop="closingDate"></el-table-column>
        <el-table-column label="发布人" prop="username"></el-table-column>
        <el-table-column label="所在分类" prop="categoryName"></el-table-column>
        <el-table-column label="具体需求" prop="requirements"></el-table-column>
        <el-table-column label="状态" prop="status">
          <template slot-scope="scope">
            <span v-if="scope.row.status === 1">待承接</span>
            <span v-if="scope.row.status === 2">已被承接</span>
            <span v-if="scope.row.status === 3">已满足</span>
            <span v-if="scope.row.status === 4">已过期</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" style="display: inline-block;" width="170px">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="修改需求信息" placement="top" :enterable="false">
              <el-button type="primary" icon="el-icon-edit" size="mini"
                         @click="showEditDialog(scope.row.demandId)"></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="删除需求" placement="top" :enterable="false">
              <el-button type="danger" icon="el-icon-delete" @click="deleteDemands(scope.row.demandId)"
                         size="mini"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>

    </el-card>

    <el-dialog
      title="修改需求信息"
      :visible.sync="editDialogVisible"
      width="50%"
      :close-on-click-modal="false"
      @close="editDialogClosed">
      <el-form :model="editForm" ref="editFormRef" label-width="70px">
        <el-form-item label="需求名称" prop="demands">
          <el-input v-model="editForm.demands" disabled></el-input>
        </el-form-item>
        <el-form-item label="所在分类" prop="categoryName">


          <el-select v-model="categoryName" clearable placeholder="请选择" @change="statusSelect">
            <div v-for="items in parentCateList">
              <el-option
                v-for="item in items.children"
                :key="item.categoryId"
                :label="item.categoryName"
                :value="item.categoryName"
              >
              </el-option>

            </div>
          </el-select>
        </el-form-item>
        <el-form-item label="具体需求" prop="editForm.demands">
          <el-input v-model="editForm.requirements" disabled></el-input>
        </el-form-item>
        <el-form-item label="需求状态" prop="editForm.status">
          <el-select v-model="statusName" clearable placeholder="请选择" @change="statusSelect">
            <el-option
              v-for="item in statusKeys"
              :key="item.status"
              :label="item.statusName"
              :value="item.status"
            >
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="editDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="changeDemands(editForm)">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
    import {
        changeDemandById,
        deleteDemandById,
        searchDemandsPage, selectDemandById
    } from "../../assets/api/demandsApi";
    import {getAllCategory} from "../../assets/api/categoryApi";

    export default {
        name:'demands',
        data() {
            return {
                demandsList: [],
                searchDemands: '',
                pageNum: 1,
                pageSize: 5,
                total: 0,
                editDialogVisible: false,
                editForm: {},
                parentCateList: [],
                statusName: '',
                status: 0,
                statusKeys: [{status: 1, statusName: '待承接'},
                    {status: 2, statusName: '已被承接'},
                    {status: 3, statusName: '已满足'},
                    {status: 4, statusName: '已过期'}],
                categoryName: ''
            }
        },
        methods: {
            getDemandsList() {
                let data = {};
                data.demands = this.searchDemands;
                searchDemandsPage(this.pageNum, this.pageSize, data).then((res) => {
                    if (res.code === 200) {
                        this.demandsList = res.data.records;
                        this.total = res.data.total;
                    }
                })
            },
            handleSizeChange(newSize) {
                this.pageSize = newSize;
                this.getDemandsList();
            },
            handleCurrentChange(newPage) {
                this.pageNum = newPage;
                this.getDemandsList();
            },
            showEditDialog(demandId) {
                this.getCategoryList();
                this.editDialogVisible = true;
                let data = {};
                data.demandId = demandId;
                selectDemandById(data).then((res) => {
                    this.editForm = res.data;
                })
            },
            getCategoryList() {
                let data = {};
                data.categoryLevel = 0;
                getAllCategory(this.pageNum, this.pageSize, data).then((res) => {
                    if (res.code !== 200) {
                        return this.$message.error(res.message);
                    }
                    this.parentCateList = res.data.records;
                })
            },
            editDialogClosed() {
                this.$refs.editFormRef.resetFields();
            },
            handleChange() {
                this.editForm.rootCategoryId = this.selectedKeys[0];
                this.editForm.categoryId = this.selectedKeys[1];
            },
            changeDemands() {
                changeDemandById(this.editForm).then((res) => {
                    if (res.code !== 200) {
                        this.$message.error(res.message);
                    }
                    this.editDialogVisible = false;
                    this.getDemandsList();
                    this.$message.success(res.message);
                })
            },
            statusSelect() {
                this.editForm.status = this.statusName;
                this.editForm.categoryName = this.categoryName;
            },
            deleteDemands(demandId) {
                const confirmResult = this.$confirm(
                    '此操作将永久删除该需求, 是否继续?',
                    '提示',
                    {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                    let data = {};
                    data.demandId = demandId;
                    deleteDemandById(data).then((res) => {
                        if (res.code !== 200) {
                            return this.$message.error(res.message);
                        }
                        this.getDemandsList();
                        this.$message.success(res.message);
                    })

                }).catch(() => {
                    this.$message.info('已取消删除');
                });

            },
        },
        created() {
            this.getDemandsList();
        },
    }
</script>

<style scoped>
  @import "../../styles/css/global.css";

</style>
